﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.IO;
using DifferenceEngine;
using System.Collections;

namespace PowerShell.Host.PsTFS.CdmLet.PsObject
{
    public class Ps2FilesDiff
    {
        public String Time { get; set; }
        public String Folder { get; set; }
        public PsFileDiff NameL { get; set; }
        public PsFileDiff NameR { get; set; }
        public TypeDiff TDiff { get; set; }
        public List<PsLineFile> StreamL { get; set; }
        public List<PsLineFile> StreamR { get; set; }
        public List<DiffResultSpan> ResultDiff { get; set; }

        public void Results(DiffList_TextFile source, DiffList_TextFile destination, ArrayList DiffLines, double seconds)
		{
			this.Time = string.Format("Results: {0} secs.",seconds.ToString("#0.00"));

            StreamL = new List<PsLineFile>();
            StreamR = new List<PsLineFile>();

            PsLineFile lviS;
            PsLineFile lviD;
			int cnt = 1;
			int i;

			foreach (DiffResultSpan drs in DiffLines)
			{				
				switch (drs.Status)
				{
					case DiffResultSpanStatus.DeleteSource:
						for (i = 0; i < drs.Length; i++)
						{
							lviS = new PsLineFile();
                            lviS.Number = cnt.ToString("00000");
							lviD = new PsLineFile();
                            lviD.Number = cnt.ToString("00000");
                            lviS.DiffResultSpanStatus = DiffResultSpanStatus.DeleteSource;
                            lviD.DiffResultSpanStatus = DiffResultSpanStatus.DeleteSource;
							
                            lviS.Line =((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line;
							lviD.Line = String.Empty;

                            StreamL.Add(lviS);
                            StreamR.Add(lviD);
							cnt++;
						}
						
						break;
					case DiffResultSpanStatus.NoChange:
						for (i = 0; i < drs.Length; i++)
						{
                            lviS = new PsLineFile();
                            lviS.Number = cnt.ToString("00000");
                            lviD = new PsLineFile();
                            lviD.Number = cnt.ToString("00000");
                            lviS.DiffResultSpanStatus = DiffResultSpanStatus.NoChange;
                            lviD.DiffResultSpanStatus = DiffResultSpanStatus.NoChange;

                            lviS.Line = ((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line;
                            lviD.Line = ((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line;

                            StreamL.Add(lviS);
                            StreamR.Add(lviD);

							cnt++;
						}
						
						break;
					case DiffResultSpanStatus.AddDestination:
						for (i = 0; i < drs.Length; i++)
						{
                            lviS = new PsLineFile();
                            lviS.Number = cnt.ToString("00000");
                            lviD = new PsLineFile();
                            lviD.Number = cnt.ToString("00000");
                            lviS.DiffResultSpanStatus = DiffResultSpanStatus.AddDestination;
                            lviD.DiffResultSpanStatus = DiffResultSpanStatus.AddDestination;

                            lviS.Line = string.Empty;
                            lviD.Line = ((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line;

                            StreamL.Add(lviS);
                            StreamR.Add(lviD);

                            cnt++;
						}
						
						break;
					case DiffResultSpanStatus.Replace:
						for (i = 0; i < drs.Length; i++)
						{
                            lviS = new PsLineFile();
                            lviS.Number = cnt.ToString("00000");
                            lviD = new PsLineFile();
                            lviD.Number = cnt.ToString("00000");
                            lviS.DiffResultSpanStatus = DiffResultSpanStatus.Replace;
                            lviD.DiffResultSpanStatus = DiffResultSpanStatus.Replace;

                            lviS.Line = ((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line;
                            lviD.Line = ((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line;

                            StreamL.Add(lviS);
                            StreamR.Add(lviD);

                            cnt++;
						}
						
						break;
				}
				
			}
		}
    }
    public class PsLineFile 
    {
        public String Number { get; set; }
        public String Line { get; set; }
        public DiffResultSpanStatus DiffResultSpanStatus { get; set; }
    }
    public enum TypeDiff
    {
        Left = 0,
        Right = 1,
        Identical = 2,
        Diff = 3,
        Other = 4
    }
    public class PsFileDiff : IDisposable
    {
        public String LocalFile { get; set; }
        public String Folder { get; set; }
        public String Name { get; set; }
        public String FolderSource { get; set; }

        private Item _item;
        public Item Item 
        {
            get
            {
                return _item;
            }
            set
            {
                _item = value;
                try
                {
                    if (_item.ItemType != ItemType.Folder)
                    {
                        int indexFile = _item.ServerItem.LastIndexOf(@"/");
                        this.Name = _item.ServerItem.Substring(indexFile + 1);
                        if (FolderSource.Length != indexFile)
                        {
                            this.Folder = _item.ServerItem.Substring(FolderSource.Length, _item.ServerItem.Length - FolderSource.Length - this.Name.Length);
                        }
                        else
                        {
                            this.Folder = string.Empty;
                        }
                    }
                    else
                    {
                        this.Folder = _item.ServerItem.Substring(FolderSource.Length);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    PsLog.AddLog(null, ex.ToString());
                }
            }
        
        }

        public String Download()
        {
            string sfile = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + @"\tempPsTFS\";
            
            this.LocalFile = sfile + this.FolderSource + @"\" + this.Name;

            Item.DownloadFile(this.LocalFile);

            return this.LocalFile;
        }

        public override string ToString()
        {
            return this.Name;
        }


        #region IDisposable Membres

        public void Dispose()
        {
            if (this.LocalFile != null)
            {
                File.Delete(this.LocalFile);
            }
        }

        #endregion
    }
}
